/* show sub navigation menu */
function subNav(){
	$("#nav li").mouseover(function(){
		$(this).children(".subnav").slideDown();
	});
}

function redirect(page){
	window.location.href = page;
}

function userRecipes(id,email,nickname){
	redirect("user-recipe.php?author="+id+"&email="+email+"&nickname="+nickname);	
}

function resize(obj, width, height){
	var old_width = $(obj).css("width");
	var old_height = $(obj).css("height");
	if(width!=height){
		if(old_width>=old_height){
			$(obj).width(width);
			$(obj).css('height','auto');	
		}else{
			$(obj).height(height);
			$(obj).css('width','auto');
		}
	}else{ // square image
		if(old_width>old_height){
			$(obj).width(width);
			$(obj).css('height','auto');	
		}else{
			$(obj).height(height);
			$(obj).css('width','auto');
		}
	}
}

/* evens out width of labels	*/
function size_labels(){
	var max_width = 0;
	$("label:not(.ignore)").each(function(){
		var curr_width = $(this).width();
		if(curr_width>max_width)
			max_width=curr_width;
	});		
	$("label:not(.ignore)").width(max_width);
}

function submitOnEnter(obj, action){
	$(obj).bind("keydown", function(event) {
      var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
      if (keycode == 13) {
         action();
         return false;
      } else  {
         return true;
      }
   });	
}

function validate(form){
	var error = false;
	$("label.required").each(function(){
		var val = $("input[name="+$(this).attr("for")+"]").val();
		if(val==null || val==""){
			$(this).addClass("error");
			error=true;	
		}
		else{
			$(this).removeClass("error");	
		}
	});
	if(error)
		return false;
	else
		return true;
}

/*
	Logos
*/
var colors = new Array("blue","darkblue","green","orange","pink","red","spearmint","yellow");
function random_logo(img, size){
	if(!size)
		size = 250;
	size = size+"x"+size;
	var color = Math.floor(Math.random()*colors.length);
	$(""+img).attr("src","images/logo/"+size+"/logo-"+colors[color]+".png");
}

/* Autocomplete */
function setAutoComplete(element,id,tags,multiple){
	
	if(id)
		element = "#"+element;
	else
		element = "."+element;
		
	function split( val ) {
		return val.split( /,\s*/ );
	}
	function extractLast( term ) {
		return split( term ).pop();
	}
	
	if(multiple){
		$( element )
		// don't navigate away from the field on tab when selecting an item
		.bind( "keydown", function( event ) {
			if ( event.keyCode === $.ui.keyCode.TAB &&
					$( this ).data( "autocomplete" ).menu.active ) {
				event.preventDefault();
			}
		})
		.autocomplete({
			minLength: 0,
			source: function( request, response ) {
				// delegate back to autocomplete, but extract the last term
				response( $.ui.autocomplete.filter(
					tags, extractLast( request.term ) ) );
			},
			focus: function() {
				// prevent value inserted on focus
				return false;
			},
			select: function( event, ui ) {
				var terms = split( this.value );
				// remove the current input
				terms.pop();
				// add the selected item
				terms.push( ui.item.value );
				// add placeholder to get the comma-and-space at the end
				terms.push( "" );
				this.value = terms.join( ", " );
				return false;
			}
		});
	}
	else{	
		$( element ).autocomplete({
			source: tags
		});
	}
}

// set carousel functionality	
function setCarousel(obj){
	$(obj).jsCarousel({ onthumbnailclick: function(src) { /*alert(src);*/ }, autoscroll: false, circular: true, masked: false, itemstodisplay: 5, orientation: 'h'});
	
	setRecipeHover(); // add hover effect to each recipe
}

function setRecipeHover(){
	$(".recipe-details").parent("div").mouseenter(function(){
		$(this).children(".recipe-details").fadeOut();
	})
	.click(function(){ // reveal recipe details on click
		window.location.href = "view-recipe.php?rid="+$(this).children("input[type=hidden]").val();				
	});
	$(".recipe-details").parent("div").mouseleave(function(){
		$(this).children(".recipe-details").fadeIn();										
	});	
}

function setFilter(){
	$("#filter").click(function(){
		filter();
	});
}

function filter(){
	var tag = $(".filter").val();
	tag = tag.replace(",","");
	var tags = tag.split(",").concat(tag.split(" "));
	var carousel = $(".filter").parent("#main").find(".recipe_carousel");
	if(tag==""){
		$(carousel).find(".thumbnail").show();
		return;
	}
	for(var i=0; i<tags.length; i++){
		$(carousel).find("."+tags[i].substr(0,1).toUpperCase()+tags[i].substr(1).toLowerCase()).show();
		$(carousel).find(".thumbnail").each(function(){
			var show = false;
			for(var j=0; j<tags.length; j++){
				if($(this).hasClass(tags[j].substr(0,1).toUpperCase()+tags[j].substr(1).toLowerCase()))
					show = true;
			}
			if(!show)
				$(this).hide();
		});
	}	
}

function scrollAndFocus(obj){
	$(obj).focus();
	scrollToObj(obj);
}

function scrollToObj(obj){
	var offset = $(obj).offset();
	window.scrollTo(offset.left,offset.top);	
}